package org.telluriumsource.ui.widget.dojo.object;

import demo.tellurium.framework.BaseDojoWidget 

import org.json.simple.JSONObject;

/**
 * Tellurium Widget object for the Dojo ComboBox widget
 *
 * @author Roger Xu (roger.xu.2007@gmail.com)
 *
 * Date: Apr 14, 2010
 * 
 */
class ComboBox extends BaseDojoWidget {

    private static final String WIDGET_NAME = ComboBox.class.getSimpleName();

    private String widgetId = null;

    @Override
    public String getWidgetName() {
        return WIDGET_NAME;
    }

    @Override
    public JSONObject toJSON() {
        return buildJSON() {jso ->
            jso.put(UI_TYPE, NAMESPACE + "_" + getWidgetName());
        }
    }

    @Override
    public void defineWidget() {
        ui.Container(uid: "${WIDGET_NAME}") {
            Container(uid: "ComboTextBox", clocator: [tag: "div", class: "dijitComboBox", widgetid: "${widgetId}"]) {
                Div(uid: "ArrowButton", clocator: [class: "dijitArrowButton"], respond: ["mouseDown", "mouseUp", "mouseEnter", "mouseLeave"])
                Container(uid: "InputField", clocator: [tag: "div", class: "dijitInputField"]) {
                    InputBox(uid: "InputTextBox", clocator: [tag: "input", type: "text"])
                }
            }
            Container(uid: "DropDownList", clocator: [tag: "div", dijitpopupparent: "${widgetId}"]) {
                List(uid: "Options", clocator: [tag: "ul"], separator: "li:visible") {
                    UrlLink(uid: "{all}", respond: ["mouseDown", "mouseUp", "mouseOver"])
                }
            }
        }
    }

    public void clickArrowButton() {
        def comboTextBox = "${WIDGET_NAME}.ComboTextBox";
        def arrowButton = "${comboTextBox}.ArrowButton";
        click arrowButton
    }

    public String getValue() {
        def comboTextBox = "${WIDGET_NAME}.ComboTextBox";
        def inputTextBox = "${comboTextBox}.InputField.InputTextBox";
        return getValue(inputTextBox);
    }

    public void selectItem(int index) {
        // click option list
        def listUid = "${WIDGET_NAME}.DropDownList.Options";
        if (!isElementPresent(listUid)) {
            console.debug("not show", listUid);
            return;
        }

        clickListOption(listUid, index);
    }
}